home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / ZMODEM / RZ.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  35KB  |  1,631 lines

  1. #define    __TOWNS__
  2. #define VERSION "3.04 02-20-91"
  3. #define PUBDIR "/usr/spool/uucppublic"
  4.  
  5. /*
  6.  * rz.c By Chuck Forsberg
  7.  *
  8.  * A program for Unix to receive files and commands from computers running
  9.  * Professional-YAM, PowerCom, YAM, IMP, or programs supporting XMODEM. rz
  10.  * uses Unix buffered input to reduce wasted CPU time.
  11.  *
  12.  *
  13.  * This version implements numerous enhancements including ZMODEM Run Length
  14.  * Encoding and variable length headers.  These features were not funded by
  15.  * the original Telenet development contract.
  16.  *
  17.  * This software may be freely used for non commercial and educational (didactic
  18.  * only) purposes.    This software may also be freely used to support file
  19.  * transfer operations to or from licensed Omen Technology products.  Any
  20.  * programs which use part or all of this software must be provided in source
  21.  * form with this notice intact except by written permission from Omen
  22.  * Technology Incorporated.
  23.  *
  24.  * Use of this software for commercial or administrative purposes except when
  25.  * exclusively limited to interfacing Omen Technology products requires a per
  26.  * port license payment of $20.00 US per port (less in quantity).  Use of
  27.  * this code by inclusion, decompilation, reverse engineering or any other
  28.  * means constitutes agreement to these conditions and acceptance of
  29.  * liability to license the materials and payment of reasonable legal costs
  30.  * necessary to enforce this license agreement.
  31.  *
  32.  *
  33.  * Omen Technology Inc            FAX: 503-621-3745 Post Office Box 4681
  34.  * Portland OR 97208
  35.  *
  36.  * This code is made available in the hope it will be useful, BUT WITHOUT ANY
  37.  * WARRANTY OF ANY KIND OR LIABILITY FOR ANY DAMAGES OF ANY KIND.
  38.  *
  39.  *
  40.  *
  41.  * Iff the program is invoked by rzCOMMAND, output is piped to "COMMAND
  42.  * filename"  (Unix only)
  43.  *
  44.  * Some systems (Venix, Coherent, Regulus) may not support tty raw mode read(2)
  45.  * the same way as Unix. ONEREAD must be defined to force one character reads
  46.  * for these systems. Added 7-01-84 CAF
  47.  *
  48.  * Alarm signal handling changed to work with 4.2 BSD 7-15-84 CAF
  49.  *
  50.  * NFGVMIN Updated 2-18-87 CAF for Xenix systems where c_cc[VMIN] doesn't work
  51.  * properly (even though it compiles without error!),
  52.  *
  53.  * SEGMENTS=n added 2-21-88 as a model for CP/M programs for CP/M-80 systems
  54.  * that cannot overlap modem and disk I/O.
  55.  *
  56.  * -DMD may be added to compiler command line to compile in Directory-creating
  57.  * routines from Public Domain TAR by John Gilmore
  58.  *
  59.  * HOWMANY may be tuned for best performance
  60.  *
  61.  * USG UNIX (3.0) ioctl conventions courtesy  Jeff Martin */
  62.  
  63.  
  64. #define LOGFILE "/tmp/rzlog"
  65. #include <stdio.h>
  66. #include <signal.h>
  67. #include <setjmp.h>
  68. #include <ctype.h>
  69. #include <errno.h>
  70. extern int    errno;
  71.  
  72. #ifdef    __TOWNS__
  73. #    include    <string.h>
  74. #    include    <stdarg.h>
  75. #    include    <stdlib.h>
  76. #    include    <direct.h>
  77. #    include    <stat.h>
  78. #    include    <sys/types.h>
  79. #    include    <sys/utime.h>
  80. #    include    <io.h>
  81. #    include    <time.h>
  82.  
  83. #    include    <splib.h>
  84. #    include    <fslib.h>
  85.  
  86. #    include    "usrlib.h"
  87. #    include    "rsctrl.h"
  88. #    include    "flib.h"
  89. #    include    "msgdat.h"
  90.  
  91. #    ifdef    __HIGHC__
  92. #        pragma    On(Align_labels);
  93. #    endif
  94.  
  95.     extern    int    RsPort;
  96. #    define    _FSTAT_IGN
  97. #    define    MD
  98. #endif
  99. #include    "prot.h"
  100. #include    "rz.h"
  101.  
  102. int         Zmodem = 0;         /* ZMODEM protocol requested */
  103. int         Nozmodem = 0;        /* If invoked as "rb" */
  104. unsigned    Baudrate = 2400;
  105. unsigned    Effbaud = 2400;
  106.  
  107. #include "rbsb.c"                /* most of the system dependent stuff here */
  108. #include "crctab.c"
  109.  
  110. FILE       *fout = NULL;
  111.  
  112. /*
  113.  * Routine to calculate the free bytes on the current file system ~0 means
  114.  * many free bytes (unknown)
  115.  */
  116. static    long        getfree(void)
  117. {
  118. #ifdef    __TOWNS__
  119.     int        drv;
  120.     int        tf, wf;
  121.  
  122.     if ( (drv = FS_getdrv()) >= 0 )
  123.     {
  124.         if ( FS_get_dskFree( drv, &tf, &wf) == NORMAL )
  125.             return (long)tf;
  126.     }
  127. #endif
  128.     return (~0L);                /* many free bytes ... */
  129. }
  130.  
  131. int         Lastrx;
  132. long        rxbytes;
  133. int         Crcflg;
  134. int         Firstsec;
  135. int         Eofseen;            /* indicates cpm eof (^Z) has been received */
  136. int         errors;
  137. int         Restricted = 0;     /* restricted; no /.. or ../ in filenames */
  138. #ifdef ONEREAD
  139. /* Sorry, Regulus and some others don't work right in raw mode! */
  140. int         Readnum = 1;        /* Number of bytes to ask for in read() from
  141.                                  * modem */
  142. #else
  143. int         Readnum = HOWMANY;    /* Number of bytes to ask for in read() from
  144.                                  * modem */
  145. #endif
  146.  
  147. #define DEFBYTL 2000000000L     /* default rx file size */
  148. long        Bytesleft;            /* number of bytes of incoming file left */
  149. long        Modtime;            /* Unix style mod time for incoming file */
  150. int         Filemode;            /* Unix style mode for incoming file */
  151. long        Totalleft;
  152. long        Filesleft;
  153. char        Pathname[PATHLEN];
  154. char       *Progname;            /* the name by which we were called */
  155.  
  156. int         Batch = 0;
  157. int         Topipe = 0;
  158. int         Verbose = 0;
  159. int         Thisbinary;         /* current file is to be received in bin mode */
  160. int         Blklen;             /* record length of received packets */
  161.  
  162. #ifdef SEGMENTS
  163. int         chinseg = 0;        /* Number of characters received in this data seg */
  164. char        secbuf[1 + (SEGMENTS + 1) * 1024];
  165. #else
  166. char        secbuf[1025];
  167. #endif
  168.  
  169.  
  170. char        linbuf[HOWMANY];
  171. int         Lleft = 0;            /* number of characters in linbuf */
  172. time_t        timep[2];
  173. char        zconv;                /* ZMODEM file conversion request */
  174. char        zmanag;             /* ZMODEM file management request */
  175. char        ztrans;             /* ZMODEM file transport request */
  176. int         Zctlesc;            /* Encode control characters */
  177. int         Zrwindow = 1400;    /* RX window size (controls garbage count) */
  178.  
  179. jmp_buf     tohere;             /* For the interrupt on RX timeout */
  180.  
  181. #define xsendline(c) sendline(c)
  182.  
  183. #include "zm.c"
  184.  
  185. #include "zmr.c"
  186.  
  187. int         tryzhdrtype = ZRINIT;        /* Header type to send corresponding
  188.                                          * to Last rx close */
  189.  
  190. #ifdef    __TOWNS__
  191. void    signal_func(int level)
  192. {
  193. }
  194.  
  195. /* called by signal interrupt or terminate to clean things up */
  196. void    bibi(int n)
  197. {
  198.     if (Zmodem)
  199.         zmputs(Attn);
  200.     canit();
  201.     mode(0);
  202.     USR_fprintf(stderr, "rz: caught signal %d; exiting", n);
  203.     cucheck();
  204.     EXIT(128 + n);
  205. }
  206. #else
  207. void    alrm(int level)
  208. {
  209.     longjmp(tohere, -1);
  210. }
  211.  
  212. /* called by signal interrupt or terminate to clean things up */
  213. bibi(int n)
  214. {
  215.     if (Zmodem)
  216.         zmputs(Attn);
  217.     canit();
  218.     mode(0);
  219.     fprintf(stderr, "rz: caught signal %d; exiting", n);
  220.     cucheck();
  221.     exit(128 + n);
  222. }
  223. #endif
  224.  
  225. void    zm_main(int argc, char *argv[])
  226. {
  227.     register char *cp;
  228.     register    npats;
  229.     char       *virgin, **patts;
  230.     int         exitcode = 0;
  231.  
  232.     Rxtimeout = 100;
  233. //    setbuf(stderr, NULL);
  234. #if    0
  235.     if ((cp = getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  236.         Restricted = TRUE;    /* Éºî└    */
  237. #endif
  238.  
  239.     from_cu();
  240.     chkinvok(virgin = argv[0]); /* if called as [-]rzCOMMAND set flag */
  241.     npats = 0;
  242.     while (--argc)
  243.     {
  244.         cp = *++argv;
  245.         if (*cp == '-')
  246.         {
  247.             while (*++cp)
  248.             {
  249.                 switch (*cp)
  250.                 {
  251.                     case '\\':
  252.                         cp[1] = toupper(cp[1]);
  253.                         continue;
  254.                     case 'c':
  255.                         Crcflg = TRUE;
  256.                         break;
  257.                     case 'e':
  258.                         Zctlesc = 1;
  259.                         break;
  260.                     case 't':
  261.                         if (--argc < 1)
  262.                         {
  263.                             usage();
  264.                         }
  265.                         Rxtimeout = atoi(*++argv);
  266.                         if (Rxtimeout < 10 || Rxtimeout > 1000)
  267.                             usage();
  268.                         break;
  269.                     case 'w':
  270.                         if (--argc < 1)
  271.                         {
  272.                             usage();
  273.                         }
  274.                         Zrwindow = atoi(*++argv);
  275.                         break;
  276.                     case 'v':
  277.                         ++Verbose;
  278.                         break;
  279.                     default:
  280.                         usage();
  281.                 }
  282.             }
  283.         } else if (!npats && argc > 0)
  284.         {
  285.             if (argv[0][0])
  286.             {
  287.                 npats = argc;
  288.                 patts = argv;
  289.             }
  290.         }
  291.     }
  292.     if (npats > 1)
  293.         usage();
  294.     if (Batch && npats)
  295.         usage();
  296. #ifndef    __TOWNS__
  297.     if (Verbose)
  298.     {
  299.         if ( freopen(LOGFILE, "a", stderr) == NULL )
  300.         {
  301.             printf("Can't open log file %s\n", LOGFILE);
  302.             exit(0200);
  303.         }
  304. //        setbuf(stderr, NULL);
  305.         USR_fprintf(stderr, "argv[0]=%s Progname=%s\n", virgin, Progname);
  306.     }
  307. #endif
  308.     if (Fromcu)
  309.     {
  310.         if (Verbose == 0)
  311.             Verbose = 2;
  312.     }
  313. #ifdef    __TOWNS__
  314.     USR_fprintf(stderr,"\n");
  315.     USR_fprintf(stderr,"rz ver.%s for TownsOS", MAIN_VER );
  316.     USR_fprintf(stderr,"    orignal rz %s\n\n", VERSION);
  317. #else
  318.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  319. #endif
  320.     mode(1);    /* save old tty stat, set